package vip.mall.modular.goodstype.controller;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import vip.mall.common.result.CommonResult;
import vip.mall.modular.goodstype.entity.GoodsType;
import vip.mall.modular.goodstype.service.GoodsTypeService;

import java.util.List;

/**
 * @author Asus
 * @Date 2025/2/10 15:16
 * @Descripition 商品类型控制器
 */
@RestController
@RequestMapping("goodsType")
@Tag(name = "商品类型业务")
public class GoodsTypeController {
    @Resource
    private GoodsTypeService goodsTypeService;

    /**
     * 添加商品类型
     */
    @PostMapping("/add")
    @Operation(summary = "添加商品类型")
    public CommonResult addGoodsType(@RequestParam("name") String name) {
        if (ObjectUtil.isEmpty(name)) {
            return CommonResult.error("添加商品类型失败");
        }
        GoodsType goodsType = new GoodsType(name);
        boolean result = goodsTypeService.save(goodsType);
        return result ? CommonResult.success() : CommonResult.error();

    }
    /**
     * 删除商品类型
     */
    @PostMapping("/delete")
    @Operation(summary = "删除商品类型")
    public CommonResult delete(@RequestParam("id") String id) {
        goodsTypeService.removeById(id);
        return CommonResult.success("删除商品类型成功");
    }
    /**
     * 修改商品类型
     */
    @PostMapping("/update")
    @Operation(summary = "修改商品类型")
    public CommonResult updateInfo(@RequestBody GoodsType goodsType) {
        goodsTypeService.updateById(goodsType);
        return CommonResult.success("修改商品类型成功");
    }

    /**
     * 查询商品类型列表
     */
    @PostMapping("/list")
    @Operation(summary = "查询商品类型列表")
    public CommonResult listGoodsList() {
        List<GoodsType> list = goodsTypeService.list();
        return CommonResult.success(list);
    }
}
